package pixelradar.map;

import pixelradar.Mw;
import pixelradar.config.Config;
import pixelradar.map.mapmode.MapMode;
import pixelradar.util.Render;

import java.awt.geom.Point2D.Double;
import java.util.Iterator;
import java.util.LinkedList;

public class Trail {
    public LinkedList<Trail.TrailMarker> trailMarkerList = new LinkedList();
    public int maxLength = 30;
    public String name;
    public boolean enabled;
    public long lastMarkerTime = 0L;
    public long intervalMillis = 5000L;
    private Mw mw;

    public Trail(Mw mw, String name) {
        this.mw = mw;
        this.name = name;
        this.enabled = Config.playerTrailEnabled;
        this.maxLength = Config.playerTrailMaxLength;
        this.intervalMillis = Config.playerTrailMarkerIntervalMills;
    }

    public void close() {
        this.trailMarkerList.clear();
    }

    public void onTick() {
        long time = System.currentTimeMillis();
        if (time - this.lastMarkerTime > this.intervalMillis) {
            this.lastMarkerTime = time;
            this.addMarker(this.mw.playerX, this.mw.playerY, this.mw.playerZ, this.mw.playerHeading);
        }

    }

    public void addMarker(double x, double y, double z, double heading) {
        this.trailMarkerList.add(new Trail.TrailMarker(x, y, z, heading));

        while (this.trailMarkerList.size() > this.maxLength) {
            this.trailMarkerList.poll();
        }

        int i = this.maxLength - this.trailMarkerList.size();

        for (Iterator var10 = this.trailMarkerList.iterator(); var10.hasNext(); ++i) {
            Trail.TrailMarker marker = (Trail.TrailMarker) var10.next();
            marker.alphaPercent = i * 100 / this.maxLength;
        }

    }

    public void draw(MapMode mapMode, MapView mapView) {
        Iterator var3 = this.trailMarkerList.iterator();

        while (var3.hasNext()) {
            Trail.TrailMarker marker = (Trail.TrailMarker) var3.next();
            marker.draw(mapMode, mapView);
        }

    }

    class TrailMarker {
        static final int borderColour = -16777216;
        static final int colour = -16711681;
        double x;
        double y;
        double z;
        double heading;
        int alphaPercent;

        public TrailMarker(double x, double y, double z, double heading) {
            this.set(x, y, z, heading);
        }

        public void set(double x, double y, double z, double heading) {
            this.x = x;
            this.y = y;
            this.z = z;
            this.heading = heading;
            this.alphaPercent = 100;
        }

        public void draw(MapMode mapMode, MapView mapView) {
            if (mapView.isBlockWithinView(this.x, this.z, mapMode.getConfig().circular)) {
                Double p = mapMode.blockXZtoScreenXY(mapView, this.x, this.z);
                Render.setColourWithAlphaPercent(-16777216, this.alphaPercent);
                Render.drawArrow(p.x, p.y, this.heading, (double) mapMode.getConfig().trailMarkerSize);
                Render.setColourWithAlphaPercent(-16711681, this.alphaPercent);
                Render.drawArrow(p.x, p.y, this.heading, (double) mapMode.getConfig().trailMarkerSize - 1.0D);
            }

        }
    }
}
